$(document).ready(function() {
    initSize();
    var layer = layui.layer,
        form = layui.form;

    //获取当前时间
    $("#time").showCurrentTime();
    //获取当前日期
    $("#date").showCurrentDate();
    //获取星期
    $("#week").html("星期" + "日一二三四五六".charAt(new Date().getDay()));


    $('.header-nav-item').click(function() {
        if (this.id == 'nav-firepm') {
            window.open('../../../FirePM/html/index/index.html')
        } else {
            if (this.id != '') {
                window.location.href = this.id.substring(4) + '.html';
            }
        }
    })
    $.each($(".header-nav-item"), function(index, el) {
        if (window.location.href.indexOf(el.id.substring(4)) > -1) {
            $('#nav-' + el.id.substring(4)).addClass('active');
        }

    });

    $('.content-left-item').click(function() {
        if (this.id != '') {
            window.location.href = this.id + '.html';
        }
    })
    $.each($(".content-left-item"), function(index, el) {
        if (window.location.href.indexOf(el.id) > -1) {
            $('#' + el.id).addClass('active');
        }

    });

    /*街道列表*/
    $('#street>span').click(function() {
        $('#streetList').animate({ height: 'toggle' }, 350);
    })
    /*点击街道切换事件*/
    $('input[name="street"]').click(function() {
        $('#street>span').html(this.value);
        townshipBoundary(this.value, this.dataset.key)
        $('#streetList').hide();
        $('#street-name').html(this.value);
    })

    /*点击可选项筛选事件*/
    $('.optional-item-checkbox-input').click(function() {
        if (this.checked) {
            if (this.id == 'lk') {
                trafficToggle()
            } else if (this.id == 'jzw') {
                loadNodePoints();
            } else {
                setIconIntoMap(this.id)
                if (nodeflag == 2) {
                    $('#nodemsg').slideDown();
                    $('#node-name').html($(this).parent().prev().text().trim());
                    $('#nodemsg-normal').html(Math.floor(Math.random() * (150 - 50 + 1) + 50));
                    $('#nodemsg-warn').html(Math.floor(Math.random() * (18 - 2 + 1) + 2));
                }
            }

        } else {
            if (this.id == 'lk') {
                trafficToggle()
            } else if (this.id == 'jzw') {
                map.remove(buildsArray);
            } else {
                clearIconIntoMap(this.id)
                if (nodeflag == 2) {
                    $('#nodemsg').slideUp();
                    $('#node-name').html('');
                    $('#nodemsg-normal').html('');
                    $('#nodemsg-warn').html('');
                }
            }

        }
    })

    $('#nodemsg-close').click(function() {
        $('#nodemsg').slideUp();
        $('#node-name').html('');
        $('#nodemsg-normal').html('');
        $('#nodemsg-warn').html('');
    })

    /*可选择项*/
    $('#optional-span,#optional-span-img').click(function() {
        $('.optional').animate({ height: 'toggle' }, 350);
    })
    /*总况*/
    $('#overview-span,#overview-span-img').click(function() {
        $('.overview').animate({ height: 'toggle' }, 350);
    })
    /*建筑物评分*/
    $('#building-rating-span,#building-rating-span-img').click(function() {
        $('.building-rating').animate({ height: 'toggle' }, 350);
    })


    //选项卡切换
    $('.header-content-bottom-right-span:eq(0),#monitoring-img').click(function(e) {
        $('.header-content-bottom-right-span:eq(1)~.header-content-bottom-right-item').slideUp(function() {
            $('.header-content-bottom-right-span:eq(2)~.header-content-bottom-right-item').slideUp(function() {
                $('.header-content-bottom-right-span:eq(0)~.header-content-bottom-right-item').animate({ height: 'toggle' }, 350);
            });
        });
    })
    $('.header-content-bottom-right-span:eq(1),#analyse-img').click(function(e) {
        $('.header-content-bottom-right-span:eq(0)~.header-content-bottom-right-item').slideUp(function() {
            $('.header-content-bottom-right-span:eq(2)~.header-content-bottom-right-item').slideUp(function() {
                $('.header-content-bottom-right-span:eq(1)~.header-content-bottom-right-item').animate({ height: 'toggle' }, 350, function() {
                    showCharts();
                });
            });
        });
    })
    $('.header-content-bottom-right-span:eq(2),#maintenance-img').click(function(e) {
        $('.header-content-bottom-right-span:eq(1)~.header-content-bottom-right-item').slideUp(function() {
            $('.header-content-bottom-right-span:eq(0)~.header-content-bottom-right-item').slideUp(function() {
                $('.header-content-bottom-right-span:eq(2)~.header-content-bottom-right-item').animate({ height: 'toggle' }, 350);
            });
        });
    })

    /*设备类型选择点击事件*/
    var nodeflag = 1;
    $('#optional-item-node').click(function() {
        if (nodeflag == 1) {
            $('#optional-item-node').css('transform', 'rotate(90deg)');
            $('#optional-select').animate({ height: 'toggle' }, 350, function() {
                $('#node-select').animate({ height: 'toggle' }, 350);
            });
            nodeflag = 2;
        } else {
            $('#optional-item-node').css('transform', 'rotate(0deg)');
            $('#node-select').animate({ height: 'toggle' }, 350, function() {
                $('#optional-select').animate({ height: 'toggle' }, 350);
            });
            nodeflag = 1;
        }

    })
})


/*初始化默认字体大小*/
function initSize() {
    //适应不同分辨率
    let fontSize;
    resize();
    window.onresize = resize;

    function resize() {

        let winW = window.innerWidth;
        if (winW >= 1440) { fontSize = winW / 1440 * 100 }
        document.getElementsByTagName('html')[0].style.cssText = "font-size:" + fontSize + "px";
    }


}


///数组排序
function getMaxSort(arr) {
    var max
    for (var i = 0; i < arr.length; i++) {
        for (var j = i; j < arr.length; j++) {
            if (arr[i] < arr[j]) {
                max = arr[j]
                arr[j] = arr[i]
                arr[i] = max
            }
        }
    }
    return arr
}


///图片自动旋转
function rotatepic(id) {
    //图片旋转
    var img = $("#" + id);
    var speed = 30;
    var deg = 0;
    setInterval(function() {
        img.css({
            '-ms-transform': 'rotate(' + deg + 'deg)',
            /* IE 9 */
            '-moz-transform': 'rotate(' + deg + 'deg)',
            /* Firefox */
            '-webkit-transform': 'rotate(' + deg + 'deg)',
            /* Safari and Chrome */
            '-o-transform': 'rotate(' + deg + 'deg)',
            /* Opera */
            'transform': 'rotate(' + deg + 'deg)'
        });
        deg += 1;
        if (deg >= 360) {
            deg = 0;
        }
    }, speed);

}



/*图片循环*/
$.fn.scrollLoop = function(options) {

    var def = $.extend({
        'time': 5000,
        'offsetX': -50,
        'deltaX': 50,
        'offsetY': 0,
        'deltaY': 0,
        'position_type': 'absolute',
        'play_count': -1
    }, options);

    if (def['play_count'] == 0)
        return this;
    else
        def['play_count']--;

    return this.css({ position: def['position_type'], left: def['offsetX'], top: def['offsetY'] }).animate({ left: def['deltaX'], top: def['deltaY'] }, def['time'], function() { $(this).scrollLoop({ 'time': def['time'], 'offsetX': def['offsetX'], 'deltaX': def['deltaX'], 'offsetY': def['offsetY'], 'deltaY': def['deltaY'], 'position_type': def['position_type'], 'play_count': def['play_count'] }); });
}





/*判断字段是否为空或者*/
function checkNullOrEmpty(field) {

    if (field == null || field === "") {
        return ''
    } else {
        return field
    }

}

/**/


/*字符长度过长处理*/
function exceedingLengthHandle(str, length) {
    if (str == null) {
        str = ''
    }
    return (str.length < length) ? str : (str.substring(0, length) + '...')

}


/*图表*/

function showCharts() {
    var charts = echarts.init(document.getElementById('analyse-chart'));
    option = {
        legend: {
            data: ['正常', '火警', '故障'],
            textStyle: {
                color: '#fff'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '10%',
            top: '20%',
            containLabel: true
        },
        color: ['#17E0BC', '#EF0D0D', '#F5B310'],
        xAxis: [{
            type: 'category',
            boundaryGap: false,
            data: ['10.1', '10.2', '10.3', '10.4', '10.5', '10.6', '10.7'],
            axisTick: {
                alignWithLabel: true
            },
            axisTick: {
                alignWithLabel: true
            },
            axisLine: {
                lineStyle: {
                    color: '#19459B',
                    width: 2
                }
            },
            axisLabel: {
                color: 'white',
                fontSize: '10'
            },
            axisTick: {
                show: false
            },
        }],
        yAxis: [{
            type: 'value',
            axisLine: {
                lineStyle: {
                    color: '#19459B',
                    width: 2
                }
            },
            splitLine: {
                lineStyle: {
                    color: '#0057D9',
                    opacity: 0.1
                }
            },
            axisLabel: {
                color: 'white',
                fontSize: '10'
            },
            axisTick: {
                show: false
            },
        }],
        series: [{
            name: '正常',
            data: [118, 124, 136, 197, 145, 125, 145],
            type: 'line',
            smooth: true
        }, {
            name: '火警',
            data: [14, 52, 42, 75, 41, 28, 13],
            type: 'line',
            smooth: true
        }, {
            name: '故障',
            data: [65, 15, 64, 41, 65, 54, 17],
            type: 'line',
            smooth: true
        }]
    };
    charts.setOption(option);
}


function logout() {
    layer.confirm('确定退出系统？', {
        btn: ['确定', '取消'] //按钮
    }, function() {
        localStorage.removeItem("indexeuid");
        if (localStorage.getItem('appid') == 15) {
            loginOut1();
        } else if (localStorage.getItem('isqx') == 1) {
            loginOut_qx();
        } else {
            loginOut();
        }
    });
}

function loginOut() {
    $.ajax({
        url: '/rest/account/logout',
        type: "post",
        contentType: "application/json",
        success: function(result) {
            localStorage.setItem('isLogin', "");
            localStorage.setItem('appid', "");
            localStorage.setItem('pId', "");


            window.location.href = "login.html";
        },
        error: function(result) {
            localStorage.setItem('isLogin', "");
            localStorage.setItem('appid', "");
            window.location.href = "login.html";
        }
    })
}


function Judgeidentity() {
    var isLogin = localStorage.getItem('isLogin');
    if (isLogin == null || isLogin == "") {
        window.location.href = "login.html";
    }
}


function ajaxPrefix() {
    return '/rest/v2'
}



function AjaxRequest(url, callBack, parms, type) {
    $.ajax({
        url: '/rest/v2' + url,
        type: type ? type : 'get',
        data: parms ? JSON.stringify(parms) : '',
        contentType: "application/json",
        success: function(result) {

            callBack(result);

        }
    });
}
//百度转高德
function bd_toGd(bd_lng, bd_lat) {
    var X_PI = Math.PI * 3000.0 / 180.0;
    var x = bd_lng - 0.0065;
    var y = bd_lat - 0.006;
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
    var gg_lng = z * Math.cos(theta);
    var gg_lat = z * Math.sin(theta);
    return { lng: gg_lng, lat: gg_lat }
}

var GPS = {
    PI: 3.14159265358979324,
    x_pi: 3.14159265358979324 * 3000.0 / 180.0,
    delta: function(lat, lon) {
        // Krasovsky 1940
        //
        // a = 6378245.0, 1/f = 298.3
        // b = a * (1 - f)
        // ee = (a^2 - b^2) / a^2;
        var a = 6378245.0; //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
        var ee = 0.00669342162296594323; //  ee: 椭球的偏心率。
        var dLat = this.transformLat(lon - 105.0, lat - 35.0);
        var dLon = this.transformLon(lon - 105.0, lat - 35.0);
        var radLat = lat / 180.0 * this.PI;
        var magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        var sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
        return { 'lat': dLat, 'lon': dLon };
    },

    //GPS---高德
    gcj_encrypt: function(wgsLat, wgsLon) {
        /*        if (this.outOfChina(wgsLat, wgsLon))
                    return { 'lng': wgsLat, 'lat': wgsLon };*/

        var d = this.delta(wgsLat, wgsLon);
        return { 'lng': wgsLat + d.lat, 'lat': wgsLon + d.lon };
    },
    outOfChina: function(lat, lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    },
    transformLat: function(x, y) {
        var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
        return ret;
    },
    transformLon: function(x, y) {
        var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
        return ret;
    }
};